﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
using Twipler.App;
using Twipler.Data.Entity.Twipler;
using Twipler.WebApp;
using Twipler.Xtensions;

namespace Settings
{
    public partial class SettingsMaster : System.Web.UI.MasterPage
    {
        protected override void OnInit(EventArgs e)
        {
            RequireUser[] requireUser = (RequireUser[])Page.GetType().GetCustomAttributes(typeof(RequireUser), true);
            if (requireUser.Length > 0)
            {
                if (Current.Visitor.IsAnonymous)
                    Response.Redirect(Current.SessionTrack.ApiCaller.UserAuthenticateUrl());

                if (Current.Visitor.HasRole(requireUser[0].AtLeast) == false)
                    Response.Redirect(Current.SessionTrack.ApiCaller.UserAuthenticateUrl());
            }

            QueryStringHash[] hash = (QueryStringHash[])Page.GetType().GetCustomAttributes(typeof(QueryStringHash), true);
            if (hash.Length > 0)
            {
                if (hash[0].Validate(Request.QueryString) == false)
                    Response.Redirect("default.aspx");
            }

            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BuildMenu();

            Current.SessionTrack.ViewingPage(HttpContext.Current.Request.Url.ToString());
        }

        private void BuildMenu()
        {
            StringBuilder html = new StringBuilder();

            AppMenu.Item[] items = AppMenu.GetMenu();
            AppMenu.Section section = items[items.Length-1].Section;

            foreach (AppMenu.Item item in items)
            {
                if (Current.Visitor.IsAnonymous || !Current.Visitor.HasRole(item.Required))
                    continue;

                if (item.Section == section)
                    continue;

                section = item.Section;
                html.AppendFormat("<a href=\"{0}\">{1}</a>", item.PageLink, section == AppMenu.Section.Root ? "Home" : section.ToString());
            }
            
            litMenu.Text = html.ToString();

            string url = Request.Url.ToString();
            if (url.IndexOf("/settings") != -1)
            {
                url = url.Substring(url.IndexOf("/settings/") + "/settings/".Length);
                url = url.Substring(0, url.IndexOf("/"));

                html = new StringBuilder();
                bool show = false;

                foreach (AppMenu.Item item in items)
                {
                    if (!Current.Visitor.HasRole(item.Required))
                        continue;

                    if (!string.Equals(item.Section.ToString(), url, StringComparison.OrdinalIgnoreCase))
                        continue;

                    html.AppendFormat("<a href=\"{0}\">{1}</a><br/>", item.PageLink, item.DisplayName);
                    show = true;
                }

                divSubOptions.Visible = show;

                litOptions.Text = html.ToString();
            }
        }
    }
}